package fi.profecons.csswindows;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * CSSWindows Framework Engine Servlet
 * @author ProfeCons Oy - Semi Arajuuri
 *
 */
public class Engine extends HttpServlet {
	private final static String FILE_UPLOAD = "FILE_UPLOAD";

	private final static String FILE_UPLOAD_STATUS = "FILE_UPLOAD_STATUS";

	private final static String IMAGE_FETCH = "IMAGE_FETCH";

	private final static String IMAGE_FETCH_BINARY = "IMAGE_FETCH_BINARY";

	private final static String FILE_FETCH_TEXT = "FILE_FETCH_TEXT";

	private final static String FILE_FETCH_BINARY = "FILE_FETCH_BINARY";

	private final static String FILE_FETCH_BINARY_DOWNLOAD = "FILE_FETCH_BINARY_DOWNLOAD";

	private final static String COMMAND = "COMMAND";
	
	private WindowApplication app;

	private State state;

	private FileHandler fileHandler;

	private CommandHandler commandHandler;
	
	/**
	 * Constructor of the object.
	 */
	public Engine() {
		super();
		this.fileHandler = new FileHandler();
		this.commandHandler = new CommandHandler();

	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
                String application = this.getInitParameter("application");
		if (action == null) {
			try{
				this.app = (WindowApplication) (Class.forName(application)).newInstance();
				commandHandler.sendInitialPage(request, response);
			}catch(ClassNotFoundException e){
				
			}catch(InstantiationException e){
				
			}catch( IllegalAccessException e){
				
			}
			
		} else if (action.equals(Engine.FILE_FETCH_TEXT)) {
			fileHandler.sendTextFile(request, response);
		} else if (action.equals(Engine.FILE_FETCH_BINARY)) {
			fileHandler.sendBinaryFile(request, response);
		} else if (action.equals(Engine.FILE_FETCH_BINARY_DOWNLOAD)) {
			fileHandler.sendBinaryFileDownload(request, response);
		} else if (action.equals(Engine.IMAGE_FETCH)) {
			fileHandler.sendImage(request, response);
		} else if (action.equals(Engine.IMAGE_FETCH_BINARY)) {
			fileHandler.sendImageFile(request, response);
		} else if (action.equals(Engine.FILE_UPLOAD_STATUS)) {
			fileHandler.fileUploadStatus(request, response);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession(true);
		synchronized (session) {
			State state = (State) session.getAttribute("state");
			if (state == null) {
				state = new State();
			}
			String action = request.getParameter("action");
			if (action != null) {
				if (action.equals(Engine.FILE_UPLOAD)) {
					fileHandler.uploadFile(request, getServletContext(), state);
				} else if (action.equals(Engine.COMMAND)) {
					commandHandler.parseXML(request, response, app, state);
				}
			}
			session.setAttribute("state", state);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occure
	 */
	public void init() throws ServletException {
		Util.setContext(getServletContext());
	}

}